package 编程能力入门1;

import java.util.HashSet;
import java.util.Set;

public class _09快乐数 {
    public boolean isHappy(int n) {
        Set<Integer> set = new HashSet<>();
        while(n!= 1 && !set.contains(n)){
            set.add(n);
            n = getNext(n);
        }
        return n == 1;
    }

   private int getNext(int n){
        int totalSum = 0;
        while(n>0){
            int d = n % 10;
            n = n / 10;
            totalSum += d * d;
        }
        return totalSum;
   }
}
